package com.magicode.portal.service.impl;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.aspectj.weaver.patterns.ThisOrTargetAnnotationPointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magicode.cms.entiy.base.Article;
import com.magicode.cms.entiy.base.Enterprise;
import com.magicode.cms.entiy.base.NewsEnterpriseRela;
import com.magicode.cms.entiy.base.NewsPersonRela;
import com.magicode.cms.entiy.base.OfflineEvents;
import com.magicode.cms.entiy.base.Program;
import com.magicode.cms.entiy.base.ProgramPersonRela;
import com.magicode.cms.entiy.base.TvFilmPerson;
import com.magicode.cms.entiy.base.WsbeClick;
import com.magicode.common.dao.ICommonDaoService;
import com.magicode.portal.service.ISecEnterpriseService;
import com.sun.xml.internal.ws.message.StringHeader;

@Service("secEnterpriseService")
@Transactional
public class SecEnterpriseServiceImpl implements ISecEnterpriseService {

	@Override
	public Enterprise get(String id) {
		String hql = " from com.magicode.cms.entiy.base.Enterprise e "
				+ " left join fetch e.mainPic "
				+ " left join fetch e.weichatMainPic "
				+ " left join fetch e.country "
				+ " left join fetch e.province "
				+ " left join fetch e.city "
				+ " left outer join fetch e.relateVideos "
				+ " where e.id=? ";
		Enterprise enterprise = null;
		try {
			enterprise = (Enterprise) this.commonDaoService.getByHQL(hql, new String[]{id});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return enterprise;
	}

	@Override
	public List<Article> getArticleList(String id) {
		List<Article> artList = new ArrayList();
		String hql = " select n from "+NewsEnterpriseRela.class.getName()+" ne  left join ne.enterprise e , "+Article.class.getName()+" n " 
				+ " where e.id = :id and ne.news = n and n.status = 2 and n.valid = 1 order by n.subcribeTime desc , ne.orderCode asc  ";
		Map<String, String> map = new HashMap<String, String>();
		map.put("id", id);
		try {
			artList = this.commonDaoService.queryList(hql, map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return artList;
	}

	/**
	 * 返回格式：
	 * List<Map<String,Object>>
	 *  	progId    					节目Id
	 * 		titleCn   					节目中文名称
	 * 		textContent  				节目正文
	 * 		mainPicUrl					节目主图片路径
	 * 		subcribeTime				节目发布时间
	 *  	clickCount					节目点击量
	 *		personList					节目相关影视人
	 *			persId                  影视人Id
	 * 			persTitle				影视人中文名称
	 * 			persClickCount			影视人点击量
	 */
	@Override
	public List<Map<String,Object>> getProgramList(String id) {
		String progId = "progId";
		String titleCn = "titleCn";
		String text = "text";
		String mainPicUrl = "mainPicUrl";
		String subcribeTime = "subcribeTime";
		String clickCount = "clickCount";
		String personList = "personList";
		String persId = "persId";
		String persTitle = "persTitle";
		String persClickCount = "persClickCount";
		
		List<Map<String, Object>> resultList = new ArrayList();
		
		String sql = " select new Map( p.id as "+progId+" , p.titleCn as "+titleCn+" , p.text as "+text+" , "
				+ " m.minPath as "+mainPicUrl+" , p.subcribeTime as "+subcribeTime+" , p.clickCount as "+clickCount+" , "
				+ " t.id as "+persId+" , t.titleCn as "+persTitle+" , t.clickCount as "+persClickCount+" ) "
				+ " from "+Program.class.getName()+" p left join p.mainPic m , "
				+ProgramPersonRela.class.getName()+" ppr left join ppr.person t , "
				+Enterprise.class.getName()+" e "
				+ " where ppr.program = p and e.id = :id and e in elements(p.enterpriseList) and p.status = 2 and p.valid = 1 " 
				+ "order by p.orderCode asc , p.subcribeTime desc , p.clickCount desc , t.clickCount desc ";
		Map<String, String> ssmap = new HashMap<String, String>();
		ssmap.put("id", id);
		try {
			List<Map<String,Object>> listp = this.commonDaoService.queryList(sql, ssmap);
			
			for(Map<String,Object> map: listp){
				String sqlId = (String) map.get(progId);
				String sqlTitle = (String) map.get(titleCn);
				byte[] sqlBytetext = (byte[]) map.get(text);
				String sqlText = "";
				//将byte类型的正文内容转化为String类型
				if(null != sqlBytetext){
					try {
						sqlText = new String(sqlBytetext, "UTF-8");
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				}
				String sqlMainPic = (String) map.get(mainPicUrl);
				Date sqlSubcribeTime = (Date) map.get(subcribeTime);
				Long sqlClickCount = (Long) map.get(clickCount);
				String sqlPersId = (String) map.get(persId);
				String sqlPersTitle = (String) map.get(persTitle);
				Long sqlPersClickCount = (Long) map.get(persClickCount);
				boolean flag = false;
				if(resultList != null){
					for(Map<String,Object> tempMap :resultList){
						String iid = (String) tempMap.get(progId);
						if(sqlId.equals(iid)){
							List<Map> pList = (List<Map>) tempMap.get(personList);
							if(pList == null){
								pList = new ArrayList();
							}
							Map perMap = new HashMap();
							perMap.put(persId, sqlPersId);
							perMap.put(persTitle, sqlPersTitle);
							perMap.put(persClickCount, sqlPersClickCount);
							pList.add(perMap);
							tempMap.put(personList, pList);
							flag = true;
							break;
						}
					}
				}
				if(flag == false){
					Map iwdMap = new HashMap();
					iwdMap.put(progId, sqlId);
					iwdMap.put(titleCn, sqlTitle);
					iwdMap.put(text, sqlText);
					iwdMap.put(mainPicUrl, sqlMainPic);
					iwdMap.put(subcribeTime, sqlSubcribeTime);
					iwdMap.put(clickCount, sqlClickCount);
					List<Map<String,Object>> persList = new ArrayList();
					Map persMap = new HashMap();
					persMap.put(persId, sqlPersId);
					persMap.put(persTitle, sqlPersTitle);
					persMap.put(persClickCount, sqlPersClickCount);
					persList.add(persMap);
					iwdMap.put(personList, persList);
					resultList.add(iwdMap);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return resultList;
	}

	@Autowired
	@Qualifier("commonDaoService")
	private ICommonDaoService commonDaoService;
}
